layui.use(['table', 'laydate'], function () {
    let table = layui.table;
    let form = layui.form;
    let layer = layui.layer;
    let layDate = layui.laydate;


    layDate.render({
        elem: '#moment',
        lang: 'en'
    });
    layDate.render({
        elem: '#moment1',
        lang: 'en'
    });


    function reloadAndClose(reload, close) {
        table.reload(reload);
        if (close != null) {
            layer.close(close);
        }
    }

    table.render({
        elem: '#templet',
        url: '/time/findPage',
        page: true,
        method: 'get',
        cols: [
            [{
                field: 'moment',
                title: "时刻",
                align: 'center',
                sort: true
            },
                {
                    field: 'messageType',
                    title: "信息类型",
                    align: 'center',
                    sort: true
                },
                {
                    field: 'event',
                    title: "事件",
                    align: 'center',
                    sort: true
                },
                {
                    field: 'messageCome',
                    title: '信息来源',
                    align: "center"
                },
                {
                    title: '操作',
                    align: 'center',
                    sort: false,
                    toolbar: '#barEdit'
                }
            ]
        ],
        limits: [1, 3, 5, 7, 9],
        limit: 3,
        done: function (res, curr, count) {
            $("[lay-id='templet']>.layui-table-page").css({
                "text-align": "center"
            });
        }
    });

    let timeId = 0;
    let openPage = null;
    let openUpdatePage = null;
    table.on('tool(pageTable)', function (obj) {
        let data = obj.data,
            layEvent = obj.event;
        switch (layEvent) {
            case 'del':
                layer.confirm('确定要删除吗', function (index) {
                    if (index != 0) {
                        $.get('/time/deleteByTimeId', {"id": data.timeId}, (result) => {
                            if (result.state == 200) {
                                layer.msg(result.message, {
                                    icon: 1,
                                    time: 3000
                                });
                                reloadAndClose('templet', index);
                            } else {
                                layer.msg(result.message, {
                                    icon: 2,
                                    time: 3000
                                });
                                reloadAndClose('templet', index);
                            }
                        });
                    }
                });
                break;
            case 'edit':
                timeId = data.timeId;
                $("input[name=moment]").val(data.moment);
                $("input[name=messageType]").val(data.messageType);
                $("input[name=event]").val(data.event);
                $("input[name=messageCome]").val(data.messageCome);
                openUpdatePage = layer.open({
                    type: 1,
                    title: '修改信息',
                    content: $("#updateTime"),
                    area: ['400px', '400px'],
                    shadeClose: false,
                    end: function () {
                        $("#updateAircraftCarrier")[0].reset();
                        $("#updateTime").hide();
                    }
                });
                break;
        }
    });


    $("#add").click(() => {
        $("#addTimeInfo")[0].reset();
        openPage = layer.open({
            type: 1,
            title: '添加信息',
            content: $("#addTime"),
            area: ['600px', '500px'],
            shadeClose: false,
            end: function () {
                $("#addTime").hide();
            }
        });
    });

    form.on('submit(updateTime)', function (data) {
        let updateAircraftCarrier = $('#updateAircraftCarrier').serialize();
        $.post('/time/updateTime?timeId=' + timeId, updateAircraftCarrier, (result) => {
            if (result.state == 200) {
                layer.msg(result.message, {
                    icon: 1,
                    time: 3000
                });
                reloadAndClose('templet', openUpdatePage);
            } else {
                layer.msg(result.message, {
                    icon: 2,
                    time: 3000
                });
                reloadAndClose('templet', null);
            }
        })
        return false;
    });
    form.on('submit(addTime)', function (data) {
        let addTimeInfo = $('#addTimeInfo').serialize();
        $.post('/time/addTime', addTimeInfo, (result) => {
            if (result.state == 200) {
                layer.msg(result.message, {
                    icon: 1,
                    time: 3000
                });
                reloadAndClose('templet', openPage);
            } else {
                layer.msg(result.message, {
                    icon: 2,
                    time: 3000
                });
                reloadAndClose('templet', null);
            }
        })
        return false;
    });
});